<?php

namespace app\api\model;

use think\Model;
use think\Request;
use think\Db;

class News extends Model
{
    protected $tableName;

    //构造方法，实例化表名
    public function __construct($tableName = '')
    {
        $this->tableName = $tableName;
    }

    /* 查询表中符合条件的所有数据
     * $alias:数据表别名
     * $where:条件（带别名条件）
     * */
    public function count($where, $alias)
    {
        $data = Db::name($this->tableName)->alias($alias)->where($where)->count();
        return $data;
    }

    /*查询列表连接user表和另一张表
     * $type:连接表
     * $where:查询条件
     * $limit:查询的条数
     * $page:查询页数
     * $condition:连表条件
     * */
    public function news_list($type, $where, $limit, $page, $condition, $order = 'w.time desc')
    {
        $data = Db::name($this->tableName)->alias('w')->field('w.*,u.username,t.name')
            ->join("$type" . " t", $condition)
            ->join('__USER__ u', 'u.uid=w.uid')
            ->where($where)->order($order)
            ->limit($limit)
            ->page($page)->select();
        return $data;
    }

    /*查询单条数据
     * $id:表的主键id
     * $basename:连表的表名，为空则不链表查询
     * $condition:连表条件
     * */
    public function news_detail($id, $basename = "", $condition = "")
    {
        $where['w.id'] = $id;
        if (empty($basename)) {
            return Db::name($this->tableName)->alias('w')->where($where)->find();
        } else {
            return Db::name($this->tableName)->alias('w')->join("$basename" . " t", $condition)->where($where)->find();
        }
    }

    /*
     * 新闻数据添加
     * */
    public function news_add()
    {
        $data['title'] = input('post.headline');
        $data['abstract'] = input('post.abstract');
        $data['type'] = input('post.newstype');
        $content = input('post.content');
		$data['content']=urldecode($content);
        $data['status'] = input('post.status');
        $preg = '/<img.*? src=[\"|\']?(.*?\\.(jpg|gif|bmp|bnp|png))[\"|\']?.*?/i';
		
        preg_match_all($preg, $data['content'], $matches);
        $pic = ($matches != null) ? $matches[1] : '';
        $data['pic'] = $pic[0];
        $data['time'] = time();
        $data['uid'] = input('post.uid');
        return Db::name($this->tableName)->insert($data);
    }

    /*
     * 新闻数据修改
     * */
    public function news_edit()
    {
        $where['id'] = input('post.id');
        $data['title'] = input('post.headline');
        $data['abstract'] = input('post.abstract');
        $data['type'] = input('post.newstype');
        $content = input('post.content');
		$data['content']=urldecode($content);
        $preg = '/<img.*? src=[\"|\']?(.*?\\.(jpg|gif|bmp|bnp|png))[\"|\']?.*?/i';
        preg_match_all($preg, $data['content'], $matches);
        $pic = ($matches != null) ? $matches[1] : '';
        $data['pic'] = $pic[0];
        $data['status'] = input('post.status');
        //$data['time']=time();
        $data['uid'] = input('post.uid');
        return Db::name($this->tableName)->where($where)->update($data);
    }/*
     * 新闻状态修改
     * $id:为单个主键id或者主键id都好连接的字符串
     * $status:1为发布；2为下架；3为上传
     * $data:返回修改成功是否
     * */
    public function news_del($id, $status)
    {
        $where = 'id in (' . $id . ')';
        switch ($status['status']) {
            case 1:
                $name = '发布';
                break;
            case 2:
                $name = '下架';
                break;
            default :
                $name = '删除';
                break;
        }
        $res = Db::name($this->tableName)->where($where)->update($status);
        $data['res'] = $res;
        $data['name'] = $name;
        return $data;
    }

    /*
    *无限极分类
     * $arr:需要分类的数组
    */
    static public function tree($arr, $pid = 0, $level = 0, $html = '|-')
    {
        $tree = array();
        foreach ($arr as $v) {
            if (intval($v['pid']) == intval($pid)) {
                $v['html'] = str_repeat($html, $level);
                $tree[] = $v;
                $tree = array_merge($tree, self::tree($arr, $v['id'], $v['level'], $html));
            }
        }
        return $tree;
    }

    /* 列表只连接user表
     * $where:查询条件
     * $limit:查询的条数
     * $page:查询页数
     * */
    public function banner_list($where, $limit, $page)
    {
        $data = Db::name($this->tableName)->alias('b')->field('b.*,u.username')
            ->join('__USER__ u', 'u.uid=b.uid')
            ->where($where)->order('b.time desc')
            ->limit($limit)
            ->page($page)->select();
        return $data;
    }

    /* 列表只连接user表
     * $files:获取的表单上传文件
     * $upload:需要移动的文件夹
     * 返回移动的文件名及地址
     * */
    public function upload($files, $upload)
    {
        $info = $files->move(ROOT_PATH . 'public' . DS . $upload);
        if ($info) {
            return $info->getSaveName();
        } else {
            return false;
        }
    }

    /* banner添加
     * $arr:其他不同的数据组成的数组
     * */
    public function banner_add($arr)
    {
        $data['uid'] = $_POST['uid'];
        $data['title'] = trim($_POST['title']);
        $data['url'] = trim($_POST['url']);
        $data['time'] = time();
        $data['sheet'] = trim(input('sheet'));
        $data['status'] = $_POST['status'];
        $result = array_merge($data, $arr);
        return Db::name($this->tableName)->insert($result);
    }

    /* banner修改
     * $arr:其他不同的数据组成的数组
     * */
    public function banner_edit($arr)
    {
        $where['id'] = input('post.id');
        $data['uid'] = input('post.uid');
        $data['title'] = trim(input('post.title'));
        $data['status'] = input('post.status');
        $data['url'] = trim(input('post.url'));
        $result = array_merge($data, $arr);
        return Db::name($this->tableName)->where($where)->update($result);
    }

    /* 查询单条或某个字段数据
     * $where:查询条件
     * $field:字段名，不填为全部数据
     * */
    public function check_name($where = [], $field = '*')
    {
        return Db::name($this->tableName)->field($field)->where($where)->find();
    }

    /* 某个字段数据增加1
     * $where:查询条件
     * $view:字段名
     * */
    public function plus($where, $view, $num = 1)
    {
        return $data = Db::name($this->tableName)->where($where)->setInc($view, $num);
    }

    /* 某个字段数据减1
     * $where:查询条件
     * $view:字段名
     * */
    public function minus($where, $view, $num = 1)
    {
        return $data = Db::name($this->tableName)->where($where)->setDec($view, $num);
    }

    /* 查询列表的所有数据
     * $where:查询条件
     * $view:字段名
     * */
    public function table_list($where, $view = "*")
    {
        return $data = Db::name($this->tableName)->where($where)->field($view)->select();
    }

    /* 查询列表的随机数据
     * $where:查询条件
     * $view:字段名
     * $limit:条数
     * */
    public function rand_list($where, $view = "*", $limit, $order = "rand()")
    {
        return Db::name($this->tableName)->field($view)
            ->where($where)->order($order)
            ->limit($limit)
            ->select();

    }

    /* 分页类
     *
     * */
    public function page($_count, $_number = 10)
    {
        $Page = new \app\api\model\Page($_count, $_number);

        return $Page;
    }

    /* 列表只连接一张表
      * $where:查询条件
      * $limit:查询的条数
      * $page:查询页数
      * */
    public function two_list($where, $type1, $condition1, $limit, $page, $fields)
    {
        $data = Db::name($this->tableName)->alias('b')->field($fields)
            ->join($type1, $condition1)
            ->where($where)->order('b.time desc')
            ->limit($limit)
            ->page($page)->select();
        return $data;
    }

    public function video_count($type1, $type2, $map, $condition1, $condition2)
    {
        return $data = Db::name($this->tableName)->alias('w')
            ->join($type1, $condition1, 'left')
            ->join($type2, $condition2, 'left')
            ->where($map)->count();
    }
}